abstract class Activity {
    String activityId
    String name
    String description
    Area area
    Integer mode=1  //define in which mode this project is visible: 0=budget only; 1=sandbox only; 2=both; default:sandbox only;
    
    static hasMany = [amountOfHour : Whoa]
    
    static belongsTo = Area
    
    static constraints = {
        activityId(size:1..20, matches:"[a-öA-Ö0-9 ]+", unique:true, blank:false)
        name(size:1..200,matches:"[a-öA-Ö0-9 ]+", blank:false, unique:true)
        description(size:0..4096)
        mode(inList:[0,1,2],nullable:false)
    }

   // abstract public void addEmployee(Employee e);

    /* delete all employees linked to this activity */
    public void delAllEmployees(){
        List<Employee> l = this.getListOfEmployees()
        for(Employee e : l){
            this.delEmployee(e)
        }
    }

    /* Delete an employee from activity */
    public void delEmployee(Employee e){
         
        if(DBConnection.isConnectionStarted() && (this instanceof Project)){
            List<Whoa> aoh
            aoh = DBConnection.getWhoaList(this)
            for(Whoa a in aoh ){
                if(a?.member.id == e.id){
                    DBConnection.deleteWhoa(a)
                    return;
                }
            }
        }
        else{
            for(Whoa a in amountOfHour ){
                if(a?.member.id == e.id){
                    this.removeFromAmountOfHour(a);
                    a.delete();
                    return;
                }
            }
        }
        
    }
    
   public List<Employee> getListOfEmployees(){
        ArrayList<Employee> l = new ArrayList<Employee>();
        
        if(DBConnection.isConnectionStarted() && (this instanceof Project)){
            List<Whoa> aoh
            aoh = DBConnection.getWhoaList(this)
            for(Whoa a in aoh){
                l.add(a?.member);
            }
        }
        else{
            for(Whoa a in amountOfHour){
                l.add(a?.member);
            }
        }

       // println("whoa:"+aoh)
       // println("amountOA:"+amountOfHour)
        return l;
   }

    public List<Employee> getListOfEmployeesByMode(Integer wantedMode){ // mode 0 for budget or 1 for sandbox
        ArrayList<Employee> l = new ArrayList<Employee>();

        if(DBConnection.isConnectionStarted() && (this instanceof Project)){
            List<Whoa> aoh
            aoh = DBConnection.getWhoaList(this)
            for(Whoa a in aoh){
                if(wantedMode==a?.member.mode ){
                    l.add(a?.member);
                }
            }
        }
        else{
            for(Whoa a in amountOfHour){
                if(wantedMode==a?.member.mode ){
                    l.add(a?.member);
                }
            }
        }
        return l;
   }

    public int getMode(Employee emp){
        int mode=1;
        for(Whoa a in amountOfHour){
            if(a?.member.id==emp.id) return a?.mode;
        }
        return mode;
    }

   abstract public Float calcTotalCost(int fromYear, int toYear);
   abstract public Float calcExpectedCost(int fromYear, int toYear);
   abstract public Float calcKnownIncome(int fromYear, int toYear);
   abstract public Float calcExpectedIncome(int fromYear, int toYear);


    public String toString(){
        return name+" ("+activityId+")";
    }

    public abstract void addEmployee(Employee e)
}
